package com.qzq.weibo.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.qzq.weibo.R;
import com.qzq.weibo.activity.UserFollowersActivity;
import com.qzq.weibo.activity.UserFriendsActivity;
import com.qzq.weibo.activity.UserStatusesActivity;
import com.qzq.weibo.entity.User;
import com.qzq.weibo.utils.Utils;
import com.qzq.weibo.utils.WeiCozzz;
import com.qzq.weibo.view.CircleImageView;

public class ProfileFragment extends Fragment {

	private static ProfileFragment mInstance;

	private CircleImageView iv_avatar;
	private TextView tv_screenName;
	private TextView tv_uid;
	private TextView tv_description;

	private LinearLayout tab_statuses;
	private LinearLayout tab_followers;
	private LinearLayout tab_friends;

	private TextView tv_statusesAccount;
	private TextView tv_followsAccount;
	private TextView tv_friendsAccount;

	private TextView tv_name;
	private TextView tv_sex;
	private TextView tv_location;
	private TextView tv_createdAt;

	private View rootView;

	private Utils utils;

	private User user;

	public static ProfileFragment getInstance() {
		if (mInstance == null) {
			mInstance = new ProfileFragment();
		}
		return mInstance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		if (rootView != null) {
			ViewGroup parent = (ViewGroup) rootView.getParent();
			if (parent != null) {
				parent.removeView(rootView);
			}
			return rootView;
		} else {

			View view = initView(inflater);
			initMember();

			rootView = view;
			return view;
		}
	}

	/** 初始化页面布局 */
	private View initView(LayoutInflater inflater) {

		View view = inflater.inflate(R.layout.fragment_profile, null);

		iv_avatar = (CircleImageView) view
				.findViewById(R.id.profile_user_info_iv_avatar);
		tv_statusesAccount = (TextView) view
				.findViewById(R.id.profile_user_info_tv_statuses_count);
		tv_followsAccount = (TextView) view
				.findViewById(R.id.profile_user_info_tv_follows_num);
		tv_friendsAccount = (TextView) view
				.findViewById(R.id.profile_user_info_tv_friends_num);
		tv_description = (TextView) view
				.findViewById(R.id.profile_user_info_tv_description);
		tv_name = (TextView) view.findViewById(R.id.profile_user_info_tv_name);
		tv_uid = (TextView) view.findViewById(R.id.profile_user_info_tv_uid);
		tv_screenName = (TextView) view
				.findViewById(R.id.profile_user_info_tv_screen_name);
		tv_sex = (TextView) view.findViewById(R.id.profile_user_info_tv_sex);
		tv_location = (TextView) view
				.findViewById(R.id.profile_user_info_tv_location);
		tv_createdAt = (TextView) view
				.findViewById(R.id.profile_user_info_tv_created_at);

		tab_statuses = (LinearLayout) view
				.findViewById(R.id.profile_user_info_ll_tab_statuses);
		tab_followers = (LinearLayout) view
				.findViewById(R.id.profile_user_info_ll_tab_followers);
		tab_friends = (LinearLayout) view
				.findViewById(R.id.profile_user_info_ll_tab_friends);

		return view;

	}

	/** 初始化成员变量 */
	private void initMember() {
		user = WeiCozzz.getInstance().getUser();
		utils = Utils.getInstance(getActivity());
		tv_name.setText(user.getScreen_name());
		tv_uid.setText(user.getIdstr());
		tv_statusesAccount.setText(String.valueOf(user.getStatuses_count()));
		tv_followsAccount.setText(String.valueOf(user.getFollowers_count()));
		tv_friendsAccount.setText(String.valueOf(user.getFriends_count()));
		if (!user.getDescription().equals("")) {
			tv_description.setText(user.getDescription());
		}
		tv_screenName.setText(user.getScreen_name());
		if (user.getGender().equals("m")) {
			tv_sex.setText("男");
		} else if (user.getGender().equals("f")) {
			tv_sex.setText("女");
		}
		tv_location.setText(user.getLocation());
		tv_createdAt.setText(String.valueOf(user.getCreated_at()));

		/** 异步加载头像 */
		utils.loadBitmap(user.getAvatar_large(), iv_avatar, 60, 60);

		/** 绑定事件监听 */
		tab_statuses.setOnClickListener(onClickTabStatusesListener);
		tab_followers.setOnClickListener(onClickTabFollowersListener);
		tab_friends.setOnClickListener(onClickTabFriendsListener);

	}

	/** tab_statuses 点击事件监听 */
	private OnClickListener onClickTabStatusesListener = new OnClickListener() {

		@Override
		public void onClick(View v) {

			Intent intent = new Intent(getActivity(),
					UserStatusesActivity.class);
			startActivity(intent);
		}
	};

	/** tab_followers 点击事件监听 */
	private OnClickListener onClickTabFollowersListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(getActivity(),
					UserFollowersActivity.class);
			startActivity(intent);
		}
	};

	/** tab_friends 点击监听 */
	private OnClickListener onClickTabFriendsListener = new OnClickListener() {

		@Override
		public void onClick(View v) {
			Intent intent = new Intent(getActivity(), UserFriendsActivity.class);
			startActivity(intent);
		}
	};
}
